<?php
   header("HTTP/1.1 303 See Other");
   $host = '';
   $port = '';
   $mydb = '';
   $user = '';
   $pass = '';
   $cset = '';
   $charsets=parse_ini_file("../resources/charsets/charsets.ini",1);
   $sini=parse_ini_file("setup.ini",1);
?>


   <form action="" method="post" enctype="multipart/form-data" >
   <table>
   <tr>
      <td>Host</td>
      <td><input type="text" name="host" class="input_style" value="<?php echo ( isset($_POST['host']) ? $_POST['host'] : $sini['config']['db_params_host'] ); ?>"/></td>
   </tr>
   <tr>
      <td>Baza</td>
      <td><input type="text" name="mydb" class="input_style" value="<?php echo ( isset($_POST['mydb']) ? $_POST['mydb'] : $sini['config']['db_params_dbname']); ?>"/></td>
   </tr>
   <tr>
      <td>Port</td>
      <td><input type="text" name="port" class="input_style" value="<?php echo ( isset($_POST['port']) ? $_POST['port'] : $sini['config']['db_params_port']); ?>"/></td>
   </tr>
   <tr>
      <td>User</td>
      <td><input type="text" name="user" class="input_style" value="<?php echo ( isset($_POST['user']) ? $_POST['user'] : $sini['config']['db_params_username']); ?>"/></td>
   </tr>
   <tr>
      <td>Hasło</td>
      <td><input type="text" name="haslo" class="input_style" value="<?php echo ( isset($_POST['haslo']) ? $_POST['haslo'] : $sini['config']['db_params_password']); ?>"/></td>
   </tr>
   <tr>
   <td>Charset</td>  <td>
      <select name="charset" class="input_style">
      <?php foreach($charsets as $row => $key) :
         $s='';
         if($_POST['charset'] != null && $_POST['charset'] == $row){
               $s='selected="selected"';
         }
      ?>
         <option <?php echo $s; ?> value="<?php echo $row; ?>"><?php echo $key;?></option>
      <?php endforeach;?>
      </select>
   </td>
   </tr>
   <tr>
      <td><input name="test" type='submit' id='test' value="Test Konfiguracji" /></td>
   </tr>
   </table>
   </form>

<?php 

	if($_POST!=NULL && ($_POST['host']!=NULL) && ($_POST['mydb'] != NULL) && ($_POST['user'] !=NULL) && ($_POST['haslo'] != NULL)){
            echo "<div class='logdiv' ><pre>";
            $host = $_POST['host'];
            $port = intval($_POST['port']);
            $mydb = $_POST['mydb'];
            $user = $_POST['user'];
            $pass = $_POST['haslo'];
            $cset = $_POST['charset'];

/*
		$error=0;
		if($port == 0){
			echo "Niepoprawny Port<br />"; 
			$error++;
		}
		if(!array_key_exists($cset,$charsets)){
			echo "Nieznane kodowanie ".$cset."<br />"; 
			$error++;
		}
		if($error >0){
			exit(0);
		}    
*/
		$link = mysql_connect($host.":".$port, $user, $pass);
		if (!$link) {
			die('Połączenie nieudane...</br> ' . mysql_error().'<br />');
		}
		//echo 'Połączenie udane...<br />';

		if (mysql_query("CREATE DATABASE IF NOT EXISTS ".$mydb,$link)){
			//echo "Baza danych stworzona...<br />";
		}
		else{
			echo "Error tworzenia bazy danych: " . mysql_error();  
			exit(0);     
		}
/*
		if(!defined('PDO::ATTR_DRIVER_NAME')) {
			//echo 'PDO nie dostępne';
			exit(0);
		}else{
			echo "PDO_MYSQL jest dostepne na twoim serwerze...<br />";
		}
*/
		try {
			//echo "Test połączenia ze sterownikiem...<br />"; 
			$dbh = new PDO ( 'mysql:host='.$host.';dbname='.$mydb.';port='.$port , $user , $pass ); 
			if($cset != null){  
				$dbh -> exec ( "SET CHARACTER SET ".$cset ); 
			}
			$dbh = null ; 

		} catch ( PDOException $e ) { 
			die("Error!: " . $e->getMessage () . "<br/>"); 
		}
		//echo "Test sterownika udany<br />"; 
		//echo "Odczyt skryptu bazy danych...<br />";

		$fh = fopen("../wdrozenie/mysql.sql", "r");
		$file='';
		if($fh){
			$file = file_get_contents("../wdrozenie/mysql.sql");
			$file = explode(';',$file);       
		}else{
			echo "błąd odczytu skryptu, brak praw dostepu... <br />";
			exit(0);
		}
		$db_selected = mysql_select_db($mydb, $link);
		if (!$db_selected) {
			die ('Nie można ustawić bazy : '.$mydb . mysql_error());
		}       
		for($i=0;$i < (count($file)-1);$i++){
			$tmp =(explode("\n",$file[$i]));
			if($file[$i] != ""){
				$result = mysql_query($file[$i]);
				if (!$result) {
					die('nieudane wykonanie skryptu bazy...<br /> '.$file[$i]. mysql_error());
				}
			}
		}
		mysql_close($link);
		$tab=Array ( 'config' => Array ( 
			'db_adapter' => "PDO_MYSQL", 
			'db_params_host' => $host,
			'db_params_port' => $port,
			'db_params_dbname' => $mydb,
			'db_params_username' => $user,
			'db_params_password' => $pass,
			'db_params_charset' =>$cset,
			'db_ok' => '1'
		));
		$n->w2i($tab, "setup.ini",TRUE);
		echo "</pre></div>";
	}
        $sini=parse_ini_file("setup.ini",1);
        if($sini['config']['db_ok'] == 1){
            echo '<form action="index.php?token='.md5($results[2]['file']).'" method="post" enctype="multipart/form-data" >';
            echo " <input type='submit' ".$str." value='dalej' /></form>";
        }



 ?>



